<?php
/**
 * Each engineer has a duty to keep the code elegant
 * Created by wolffy.
 * Author @wolffy<13750088090@sina.cn>
 * Date: 2018/8/12
 * Time: 11:40
 */
namespace App\Repositories;

use App\Models\RepaymentBill;

class RepaymentBillRepository
{
    /**
     * 创建用户绑定的还款账单
     * @param array $params
     * @return mixed
     */
    public function create(array $params)
    {
        return RepaymentBill::create($params);
    }

    /**
     * 根据id获取还款账单资料
     * @param $id
     * @return mixed
     */
    public function byId($id)
    {
        return RepaymentBill::find($id);
    }

    public function getRepaymentBill($userId, $id, $monthBillDate)
    {
        return RepaymentBill::where([
            'user_id' => $userId,
            'credit_card_id' => $id,
            'month_bill_date' => $monthBillDate
        ])->first();
    }

    public function updateDate($userId, $id, $monthBillDate, $lastBillDate, $billDate, $repaymentDate)
    {
        return RepaymentBill::where([
            'user_id' => $userId,
            'credit_card_id' => $id,
            'month_bill_date' => $monthBillDate
        ])->update([
            'last_bill_date' => $lastBillDate,
            'bill_date' => $billDate,
            'repayment_date' => $repaymentDate,
            'update_time' => time()
        ]);
    }

    public function update($userId, $id, $address, $repaymentMoney)
    {
        return RepaymentBill::where([
            'user_id' => $userId,
            'credit_card_id' => $id,
            'month_bill_date' => date('Y-m')
        ])->update([
            'consume_address' => $address,
            'repayment_bill_money' => $repaymentMoney,
            'update_time' => time()
        ]);
    }

    /**
     * 获取还款记录
     */
    public function getRepaymentRecord($userId, $creditCardId)
    {
        return RepaymentBill::where([
                'user_id' => $userId,
                'credit_card_id' => $creditCardId,
            ])
            ->orderBy('month_bill_date', 'desc')
            ->select('id as bill_id', 'month_bill_date','repayment_bill_money','last_bill_date','bill_date','repayment_date','bill_status')
            ->get()->toArray();
    }

    public function getRepaymentBillAll($userId, $creditCardId)
    {
        return RepaymentBill::where([
            'user_id' => $userId,
            'credit_card_id' => $creditCardId,
        ])
            ->orderBy('month_bill_date', 'desc')
            ->select('id as bill_id')
            ->get();
    }
}